package com.changgou.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.item.service.PageService;
import com.changgou.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author tang.qingbo
 * @Date 2020/8/30 23:13
 * @Version 1.0
 */
@Service
public class PageServiceImpl implements PageService {

    //静态文件保存位置,从配置文件中获取
    @Value("${pagepath}")
    private String pagepath;

    //注入模板引擎对象
    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    /**
     * 根据spuId生成静态页面
     *
     * @param spuId
     */
    @Override
    public void createPageHtml(String spuId) {
        /**
         * 查询所有的spu 批量生产静态页面
         */
        List<Spu> spuList = spuFeign.findAll().getData();
        if (spuList != null) {
            for (Spu spu : spuList) {
                try {
                    //创建上下文对象
                    Context context = new Context();
                    //定义数据模型
                    Map<String, Object> dataModel = buildDataModel(spu.getId());
                    context.setVariables(dataModel);

                    //生成静态页面位置
                    File destFile = new File(pagepath, spu.getId() + ".html");
                    //生成静态页面
                    PrintWriter printWriter = new PrintWriter(destFile);

                    /**
                     * 输出文件
                     *  参数1: 模板名称
                     *  参数2: 上下文对象
                     *  参数3: 文件输出流对象
                     */
                    templateEngine.process("item", context, printWriter);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取生成静态页面的数据
     * @param spuId
     * @return
     */
    private Map<String, Object> buildDataModel(String spuId) {
        //获取spu数据
        Result<Spu> spuResult = spuFeign.findById(spuId);
        Spu spu = spuResult.getData();

        //获取sku数据
        Sku sku = new Sku();
        sku.setSpuId(spuId);
        Result<List<Sku>> skuResult = skuFeign.findList(sku);
        List<Sku> skuList = skuResult.getData();

        //获取category数据
        Result<Category> categoryResult1 = categoryFeign.findById(spu.getCategory1Id());
        Result<Category> categoryResult2 = categoryFeign.findById(spu.getCategory2Id());
        Result<Category> categoryResult3 = categoryFeign.findById(spu.getCategory3Id());
        Category category1 = categoryResult1.getData();
        Category category2 = categoryResult2.getData();
        Category category3 = categoryResult3.getData();

        //定义集合,封装获取到的数据
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("spu", spu);
        resultMap.put("skuList", skuList);
        resultMap.put("category1", category1);
        resultMap.put("category2", category2);
        resultMap.put("category3", category3);

        //封装图片数据,多张图片,需要切割
        resultMap.put("imageList", spu.getImages().split(","));

        //封装spec规格数据
        resultMap.put("specList", JSON.parseObject(spu.getSpecItems(), Map.class));

        return resultMap;
    }
}
